package com.veal98.tree;

import com.veal98.bean.TreeNode;

import java.util.Scanner;

public class ConvertSortedArrayToBinarySearchTree_108 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i ++) {
            nums[i] = sc.nextInt();
        }

        sortedArrayToBST(nums);
    }

    public static TreeNode sortedArrayToBST(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }

        // 左闭右闭
        return generate(nums, 0, nums.length);
    }

    private static TreeNode generate(int[] nums, int left, int right) {
        if (left >= right) {
            return null;
        }

        // 根节点
        int mid = (left + right) / 2;
        TreeNode root = new TreeNode(nums[mid]);

        root.left = generate(nums, left, mid);
        root.right = generate(nums, mid + 1, right);

        return root;
    }

}
